<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background: palegoldenrod;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
    <script src="../util.js"></script>
</head>
<body>
    <div></div>
    <script>
        class Move {
            constructor () {
                this.box = document.querySelector('div')
                this.bindEvent()
            }
            bindEvent () {
                document.onkeydown = (e) => {
                    var keycode =  e.keyCode || e.which
                    console.log(keycode)
                    this.run(keycode)
                }
            }
            run (keycode) {
                    switch (keycode) {
                        case 65 :
                            var left = this.box.offsetLeft
                            this.box.style.left = left - 5 + 'px'
                        break
                        case 68 :
                            var left = this.box.offsetLeft
                            
                            this.box.style.left = left + 5 + 'px'
                        break
                        case 87 :
                            var top = this.box.offsetTop
                            
                            this.box.style.top = top - 5 + 'px'
                        break
                        case 83 :
                            var top = this.box.offsetTop
                            
                            this.box.style.top = top + 5 + 'px'
                        break
                    }
            }
        }
        new Move()
    </script>
</body>
</html>